package com.laiyizhan.cms.first.service.impl;
import com.laiyizhan.cms.first.request.FifthRequest;
import com.laiyizhan.cms.first.service.IFifthService;
import com.laiyizhan.common.mapper.*;
import com.laiyizhan.common.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static org.springframework.beans.BeanUtils.copyProperties;

/**
 * Created by SoulWolf on 2017/7/15.
 */

@Service
public class FifthServiceImpl implements IFifthService {

    @Autowired
    StateMapper stateMapper;
    @Autowired
    Fifth_messageMapper fifth_messageMapper;
    @Autowired
    Fifth_aMapper fifth_aMapper;
    @Autowired
    Fifth_bMapper fifth_bMapper;
    @Autowired
    Fifth_cMapper fifth_cMapper;
    @Autowired
    Fifth_dMapper fifth_dMapper;
    @Autowired
    Fifth_eMapper fifth_eMapper;

    public State Fifth_index(FifthRequest request) {
        State temp = stateMapper.selectByName(request.getoName());
        if (temp == null) {
            State state = new State();
            state.setHospital(request.getoName());
            state.setA("0");
            state.setB("0");
            state.setC("0");
            state.setD("0");
            state.setE("0");
            state.setF("0");
            state.setG("0");
            stateMapper.insertSelective(state);
            return state;
        }else {
            return stateMapper.selectByName(request.getoName());
        }
    }

    private String[] a_c = {"高级职称","中级职称","初级职称","其他","合计"};
    private String[] a_h = {"年平均在职职工人数（人）","卫生技术人员（人）","其他技术人员（人）","管理人员（人）","工勤技能人员 (人)","临时工（人）（选填）","在职职工年人均工资（万元/人）"};

    private String[] b_c = {"各级政府","国际组织","国际社会组织"};

    private String[] c_h = {"年度收费总额（元）","服务量（人次）","次均收费水平（元/人次）","卫生材料支出（元）","药品支出（元）","其他支出（元）"};


    public String Fifth_a(FifthRequest request){
        Check_message("a",request);
        /*存入个人信息*/
        Fifth_message fifth_message = new Fifth_message();
        copyProperties
                (request,fifth_message);
        fifth_messageMapper.insertSelective(fifth_message);

       /*数据处理*/
        float[] temp =  new float[35];
        for(int i=0;i<35;i++){
            if (request.getFifthA()[i]!=""){
                temp[i] = Float.parseFloat(request.getFifthA()[i]);
            }
        }
        /*计算前五行的第一列合计*/
        for(int l=0;l<28;l=l+7){
            temp[l]=temp[l+1]+temp[l+2]+temp[l+3]+temp[l+4];
               /*新添加的验证，当人数不为0的时候，工资不能为0*/
            if(temp[l]!=0 || temp[l+5]!=0 ){
                if( temp[l+6]==0){
                    return "no";
                }
            }
        }
        /*计算第六行的前五列合计*/
        for(int i=0;i<6;i++){
            temp[i+28]=temp[i+7]+temp[i+14]+temp[i+21]+temp[i];
        }


        /*生成对象，存入数据库*/
        int num=0;
        List<Fifth_a> list = new ArrayList<Fifth_a>();
        for(int i=0;i<5;i++) {
            for (int j = 0; j < 7; j++) {
                Fifth_a fifth_a = new Fifth_a();
                fifth_a.setName(request.getoName());
                fifth_a.setType(a_c[i]);
                fifth_a.setcType(a_h[j]);
                fifth_a.setCount(String.valueOf(temp[num]));
                list.add(fifth_a);
                num=num+1;
            }

        }


        fifth_aMapper.insertBatch(list);
        Change_state("a",request.getoName());
        return "ok";
    }

    public String Fifth_b(FifthRequest request){
        Check_message("b",request);
        if(request.getFifthB()!=null) {
            request.getFifthB()[0] = "本机构财政项目支出补助收入";
        }
        /*数据处理*/
        float b_count=0F;
        for(int i=1;i<162;i=i+2){
            if(request.getFifthB()[i]!=""){
                b_count=b_count+Float.parseFloat(request.getFifthB()[i]);
            }
        }


        List<Fifth_b> list = new ArrayList<Fifth_b>();
        if(request.getFifthB()!=null){
            request.getFifthB()[1]=String.valueOf(b_count);
            for(int j=0;j<182;j=j+2){
                Fifth_b fifth_b = new Fifth_b();
                /*医院名称固定，由前一页传递过来*/
                fifth_b.setHosptial(request.getoName());
                if(j<162){
                    fifth_b.setSource(b_c[0]);
                }else if(j<172){
                    fifth_b.setSource(b_c[1]);
                }else {
                    fifth_b.setSource(b_c[2]);
                }
                fifth_b.setProjectName(request.getFifthB()[j]);
                fifth_b.setCount(request.getFifthB()[j+1]);
                list.add(fifth_b);
            }
            fifth_bMapper.insertBatch(list);;
            Change_state("b",request.getoName());
        }
        return "ok";
    }

    public String Fifth_c(FifthRequest request){
        Check_message("c",request);
        /*先把前台传过来的数据整合*/
        String [] all_fifth_c = new String[444];//字符串型的所有数据
        String[] all_out_c = new String [74];
        float[] c_f = new float[444];//浮点型的所有数据，汉字的部分默认0.0
        float[] new_out = new float[74];//新增的数据 支出情况
        float[] all_charge = new float[6];//收费总额，新加的第一行，=各大项之和
        for(int i = 0;i<444;i++){
            if(i<252){all_fifth_c[i]=request.getFifthC()[i];}
            else{all_fifth_c[i]=request.getFifthCo()[i-252];}
            if((i%6)!=0 && all_fifth_c[i]!=""){c_f[i]=Float.parseFloat(all_fifth_c[i]);}
        }
        for(int w=1;w<74;w++){
            all_out_c[w]=request.getFifthCl()[w];
            if(all_out_c[w]!=null && all_out_c[w]!="" ){new_out[w]=Float.parseFloat(request.getFifthCl()[w]);}
        }
        /*计算必须项的合计数据*/

        /*计算第一列的合计*/
        for(int m=0;m<444;m=m+6){
            if(all_fifth_c[m]!=""){
                c_f[m+1]=c_f[m+2]*c_f[m+3];
            }
        }
        /*计算行的合计*/
        int[] start = {1,25,79,127,193,235,337};
        int[] round = {3,8,7,8,6,16,6};
        int[] out = {0,4,13,21,32,39,56};
        for(int i=0;i<7;i++){
            for(int j=0;j<5;j++){
                for(int k=0;k<round[i];k++){
                    c_f[start[i]+j]=c_f[start[i]+j]+c_f[(start[i]+j)+6*(k+1)];
                }
            }
            for(int m=0;m<round[i];m++){
                new_out[out[i]]=new_out[out[i]]+new_out[out[i]+m+1];
            }
        }
        /*计算全部公共卫生服务*/
        int[] to_charge = {1,25,79,127,181,187,193,235,337,379,385,391,397,403,409};
        int[] out_charge={0,4,13,21,30,31,32,39,56,63,64,65,66,67,68};
        for(int s=0;s<5;s++){
            for(int y=0;y<15;y++){
            all_charge[s]=all_charge[s]+c_f[to_charge[y]+s];
            if(s==0){
                all_charge[5]=all_charge[5]+new_out[out_charge[y]];
                 }
            }
        }


        /*转为字符串类型赋值给all_fifth_c*/
        for(int n=0;n<444;n++){
            if((n%6)!=0 && n<414){
                all_fifth_c[n]=String.valueOf(c_f[n]);
            }
            if(n>413 && (n%6)==0 && all_fifth_c[n]!=""){
                for(int p=1;p<6;p++){
                    all_fifth_c[n+p]=String.valueOf(c_f[n+p]);
                }
            }
            if(n<69){all_out_c[n]=String.valueOf(new_out[n]);}
        }
        for(int u=0;u<7;u++){
            all_fifth_c[start[u]+1]="";
        }
        for(int u=0;u<7;u++){
            all_fifth_c[start[u]+2]="";
        }

        /*将数据生成对象存储到数据库*/
        List<Fifth_c> list = new ArrayList<Fifth_c>();
        /*将全部公共卫生服务放入列表内*/
        for(int i=0;i<6;i++){
            Fifth_c fifth_c = new Fifth_c();
            fifth_c.setHospital(request.getoName());
            fifth_c.setDepartment(request.getFifthCdepartment());
            fifth_c.setType("全部公共卫生");
            if(i==2){fifth_c.setCount("");}else{
                fifth_c.setCount(String.valueOf(all_charge[i]));
            }
            fifth_c.setcType(c_h[i]);
            list.add(fifth_c);
        }
        for(int i=0;i<444;i=i+6){
            for(int j=0;j<5;j++) {
                Fifth_c fifth_c = new Fifth_c();
                fifth_c.setType(all_fifth_c[i]);
                fifth_c.setHospital(request.getoName());
                fifth_c.setDepartment(request.getFifthCdepartment());
                fifth_c.setcType(c_h[j]);
                fifth_c.setCount(all_fifth_c[i+j+1]);
                list.add(fifth_c);
            }
            /*增加 支出列至list*/
            Fifth_c fifth_c = new Fifth_c();
            fifth_c.setType(all_fifth_c[i]);
            fifth_c.setHospital(request.getoName());
            fifth_c.setDepartment(request.getFifthCdepartment());
            fifth_c.setcType("其他支出（元）");
            fifth_c.setCount(String.valueOf(all_out_c[i/6]));
            list.add(fifth_c);
        }
        fifth_cMapper.insertBatch(list);;
        Change_state("c",request.getoName());
        return "ok";
    }

    public String Fifth_d(FifthRequest request) {
        Check_message("d",request);
        List<Fifth_d> list = new ArrayList<Fifth_d>();
        if(request.getFifthD()!=null){
            for(int i=0;i<40;i=i+2){
                Fifth_d fifth_d = new Fifth_d();
                fifth_d.setHospital(request.getoName());
                fifth_d.setName(request.getFifthD()[i]);
                if (i < 30) {
                    if(request.getFifthD()[i+1]!=""){fifth_d.setCount(request.getFifthD()[i+1]);}
                    else{fifth_d.setCount("0");}
                }else{
                    fifth_d.setCount(request.getFifthD()[i+1]);
                }

                list.add(fifth_d);
            }
        }
        fifth_dMapper.insertBatch(list);
        Change_state("d",request.getoName());
        return "ok";
    }

    public String Fifth_e(FifthRequest request) {
        Check_message("e",request);
        List<Fifth_e> list = new ArrayList<Fifth_e>();

        for (int i = 0; i < 104; i = i + 4) {
            Fifth_e fifth_e = new Fifth_e();
            fifth_e.setHospital(request.getoName());
            fifth_e.setName(request.getFifthE()[i]);
            fifth_e.setType(request.getFifthE()[i + 1]);
            if (i < 44) {
                if(request.getFifthE()[i+2]!=""){
                    fifth_e.setCount(request.getFifthE()[i+2]);
                } else{
                    fifth_e.setCount("0.0");
                }
                if(request.getFifthE()[i+3]!="") {
                    fifth_e.setPrice(request.getFifthE()[i + 3]);
                }else{
                    fifth_e.setPrice("0.0");
                }
            }else{
                fifth_e.setCount(request.getFifthE()[i+2]);
                fifth_e.setPrice(request.getFifthE()[i + 3]);
            }
            list.add(fifth_e);
        }
        fifth_eMapper.insertBatch(list);
        Change_state("e",request.getoName());
        return "ok";
    }

    public String Check_message(String table,FifthRequest request){
        switch (table)
        {
            case "a":
                if(fifth_aMapper.selectByName(request.getoName())!=null){
                    fifth_aMapper.deleteByName(request.getoName());
                }
                break;
            case "b":
                if(fifth_bMapper.selectByName(request.getoName())!=null){
                    fifth_bMapper.deleteByName(request.getoName());
                }
                break;
            case "c":
                Fifth_c fifth_c = new Fifth_c();
                fifth_c.setHospital(request.getoName());
                fifth_c.setDepartment(request.getFifthCdepartment());
                if(fifth_cMapper.selectByName(fifth_c)!=null){
                    fifth_cMapper.deleteByName(fifth_c);
                }
                break;
            case "d":
                if(fifth_dMapper.selectByName(request.getoName())!=null){
                    fifth_dMapper.deleteByName(request.getoName());
                }
                break;
            case "e":
                if(fifth_eMapper.selectByName(request.getoName())!=null){
                    fifth_eMapper.deleteByName(request.getoName());
                }
                break;
        }

        return "ok";
    }


    private String Change_state(String table,String oName) {
        State state = stateMapper.selectByName(oName);
        switch (table)
        {
            case "a":
                state.setA("1");
                break;
            case "b":
                state.setB("1");
                break;
            case "c":
                state.setC("1");
                break;
            case "d":
                state.setD("1");
                break;
            case "e":
                state.setE("1");
                break;
            case "f":
                state.setF("1");
                break;
            case "g":
                state.setG("1");
                break;
        }
        stateMapper.updateByName(state);
        return "ok";
    }






}
